package com.itcpeng.controller;

import com.itcpeng.entity.Result;
import com.itcpeng.exception.MyException;
import org.springframework.expression.AccessException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;



/**
 * @RestControllerAdvice:拦截controller抛出的异常
 */
@RestControllerAdvice
public class MyExceptionHandler {
    //@ExceptionHandler:声明捕获的异常类型
    @ExceptionHandler(MyException.class)
    public Result handlerMyException(MyException my) {
        my.printStackTrace();
        //包装返回的结果
        return new Result(false,my.getLocalizedMessage());
    }

    @ExceptionHandler(Exception.class)
    public Result handlerException(Exception unknown) {
        unknown.printStackTrace();
        //包装返回的结果
        return new Result(false, "发生异常了");
    }

    @ExceptionHandler(AccessException.class)
    public Result noAccessHander(AccessException accessDeniedException) {
        return new Result(false, "权限不足");
    }

}
